$.ajaxSetup({
    contentType: "application/x-www-form-urlencoded;charset=utf-8",
    cache: false,
    complete: function(XMLHttpRequest, textStatus){
        var filterStatus = XMLHttpRequest.getResponseHeader("filter-status");
        if ("timeout" == filterStatus) {
            SessionTimeOut();
        }else if("non-privileged" == filterStatus){
            nonPrivileged(XMLHttpRequest.getResponseHeader("filter-url"));
        }
    }
});

function nonPrivileged(url) {
    alert("抱歉，暂无权限！< " +url+ " >");
}
function SessionTimeOut() {
    alert("您的会话已经过期，请重新登陆后继续操作！");
    location.href=BasePath+"/";
}

var accessControllers = "";/**可访问的页面，对应controllers.js文件**/
var accessElement = "";/**页面按钮、操作等显示控制**/
var minMenu = "0";/**页面缩小菜单按钮状态标识**/

$.ajax({
    type : "POST",
    url : BasePath + "/menu/query.do",
    dataType : "json",
    async : false,
    success : function(data) {
        var html = "<h5 class='sidebartitle'>菜单</h5><ul class='nav nav-pills nav-stacked nav-bracket' id='nav-left'>";
        var list = data.result;
        var noChildrenIds = new Array();/**没有子菜单的标签数组**/
        accessControllers = data.accessControllers;
        accessElement = data.accessElement;
        for( var i = 0; i < list.length; i++ ){
            var childrenList = list[i].childList;
            var id = list[i].code;
            if(childrenList == null || childrenList == ""){
                var path = list[i].path;
                html = html + "<li id='"+id+"'><a href='"+path+"'><i class='"+list[i].iocn+"'></i> <span>"+list[i].name+"</span></a></li>";
                noChildrenIds[noChildrenIds.length] = id;
            }else{
                html = html + "<li class='nav-parent' id='"+id+"'><a href=''><i class='"+list[i].iocn+"'></i> <span>"+list[i].name+"</span></a><ul class='children' id='"+id+"Children'>";
                for( var j = 0; j < childrenList.length; j++ ){
                    var path = childrenList[j].path;
                    html = html + "<li><a href='"+path+"'><i class='"+childrenList[j].iocn+"'></i> "+childrenList[j].name+"</a></li>";
                }
                html = html + "</ul></li>";
            }
        }
        html = html + "</ul>";
        $('#leftpanelinner').html(html);
        $("#nav-left").children().click(function() {
            if(minMenu == "0"){
                $(this).addClass("active").siblings().removeClass("active");
            }else{
                $(this).addClass("active").siblings().removeClass("active nav-active");
            }
        });
        for (x in noChildrenIds){
            $("#"+noChildrenIds[x]).children().click(function() {
                $(".nav-parent").removeClass("active nav-active");
                if(minMenu == "0"){
                    $(".children").hide();
                }
            });
        }

        var loginName=data['userInfo']['loginname'];
        sessionStorage.setItem("loginName",loginName);
        sessionStorage.setItem("loginUserName",data['userInfo']['username']);
        sessionStorage.setItem("loginPhone",data['userInfo']['mobilephone']);
        sessionStorage.setItem("loginEmail",data['userInfo']['email']);
        sessionStorage.setItem("loginRoleName",data['userInfo']['rolename']);
        sessionStorage.setItem("loginCompanyName",data['userInfo']['companyname']);
        $('#loginName').html(loginName);
    }
});

$('#minMenu').click(function(){
    if(minMenu == "0"){
        minMenu = "1";
    }else{
        minMenu = "0";
    }
});